package com.six666.medicaldevicetestingsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.six666.medicaldevicetestingsystem.dto.Observe_2_DTO;
import com.six666.medicaldevicetestingsystem.dto.Observe_DTO;
import com.six666.medicaldevicetestingsystem.dto.Peopleexcel_DTO;
import com.six666.medicaldevicetestingsystem.entity.*;
import com.six666.medicaldevicetestingsystem.mapper.ProjectOfficers_Mapper;
import com.six666.medicaldevicetestingsystem.service.ProjectFile_Service;
import com.six666.medicaldevicetestingsystem.service.ProjectOfficers_Service;
import com.six666.medicaldevicetestingsystem.service.Account_Service;

import com.six666.medicaldevicetestingsystem.service.Project_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ProjectOfficers_Service_Impl extends ServiceImpl<ProjectOfficers_Mapper, ProjectOfficers> implements ProjectOfficers_Service {
    @Autowired
    ProjectFile_Service projectFileService;
    @Autowired
    Account_Service accountService;
    @Autowired
    Project_Service projectService;
    @Override
    public int resempty(int project_id, int userid) {
        int res = 0 ;
        if(projectFileService.isempty(project_id)==1){
            res = 1;
        }
        else if (projectFileService.isempty(project_id)==2){
            res = 2;
        }
        return res;
    }

    @Override
    public int People(int project_id,int userid){
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("AccountID",userid);
        ProjectOfficers res = getOne(queryWrapper);
        String training = res.getTraining();
        int is_ok;
        if(training.equals("Finish")) {
            is_ok = 1;
        }
        else {
            is_ok = 2;
        }
        return is_ok;
    }

    @Override
    public Boolean updateAssessmentPaper(int project_id, int userid, int assessment_id) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentPaper",assessment_id);
        return update(updateWrapper);
    }

    @Override
    public Boolean updateAssessmentVideo(int project_id, int userid, int assessment_id) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentVideo",assessment_id);
        return update(updateWrapper);
    }

    @Override
    public List<String> getPaper(int project_id) {
        List<String> res = new ArrayList<>();
        for(int i =0 ; i<projectFileService.get_file_by_project_id_and_type_plus(project_id,"Paper").size();i++) {
            res.add(projectFileService.get_file_by_project_id_and_type_plus(project_id, "Paper").get(i).getFileaddress());
        }
        return res;
    }

    @Override
    public List<String> getTrainVideo(int project_id) {
        List<String> res = new ArrayList<>();
        for(int i =0 ; i<projectFileService.get_file_by_project_id_and_type_plus(project_id,"TrainVideo").size();i++) {
            res.add(projectFileService.get_file_by_project_id_and_type_plus(project_id, "TrainVideo").get(i).getFileaddress());
        }
        return res;
    }

    @Override
    public List<ProjectOfficers> getauthorize(int project_id) {
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("examination","Ongoing");
        queryWrapper.eq("AssessmentVideoStatus","Pass");
        queryWrapper.eq("AssessmentPaperStatus","Pass");
        return list(queryWrapper);
    }

    @Override
    public ProjectOfficers gettask(int project_id,int user_id) {
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("AccountID",user_id);
        return getOne(queryWrapper);
    }

    @Override
    public List<Observe_DTO> getObserve(int project_id, int userid) {
        List<Observe_DTO> dto = new ArrayList<>();
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        if(userid==0){
            queryWrapper.eq("ProjectID",project_id);
            List<ProjectOfficers> res = list(queryWrapper);
            for (ProjectOfficers re : res) {
                Observe_DTO dto1 = new Observe_DTO();
                dto1.setExamination(re.getExamination());
                dto1.setTraining(re.getTraining());
                dto1.setUser_id(re.getAccountid());
                Observe_DTO dto2;
                dto2 = accountService.observe(re.getAccountid());
                dto1.setContact(dto2.getContact());
                dto1.setName(dto2.getName());
                dto1.setAssessmentVideoStatus(re.getAssessmentvideostatus());
                dto1.setAssessmentPaperStatus(re.getAssessmentpaperstatus());
                dto.add(dto1);
            }
        }
        else {
            Observe_DTO dto1 = new Observe_DTO();
            queryWrapper.eq("ProjectID",project_id);
            queryWrapper.eq("AccountID",userid);
            dto1.setExamination(getOne(queryWrapper).getExamination());
            dto1.setTraining(getOne(queryWrapper).getTraining());
            dto1.setUser_id(userid);
            Observe_DTO dto2;
            dto2 = accountService.observe(userid);
            dto1.setAssessmentVideoStatus(getOne(queryWrapper).getAssessmentvideostatus());
            dto1.setAssessmentPaperStatus(getOne(queryWrapper).getAssessmentpaperstatus());
            dto1.setContact(dto2.getContact());
            dto1.setName(dto2.getName());
            dto.add(dto1);
        }
        return dto;
    }

    @Override
    public Date PeopleDate(int project_id, int userid) {
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("AccountID",userid);
        ProjectOfficers res = getOne(queryWrapper);
        return res.getTrainingendtime();
    }

    @Override
    public List<Map<String, String>> get_excel() {
        QueryWrapper<ProjectOfficers> queryWrapper = new QueryWrapper<>();
        List<ProjectOfficers>res = list(queryWrapper);
        List<Peopleexcel_DTO> dto = new ArrayList<>();
        List<Map<String, String>> texp = new ArrayList<>();
        for (ProjectOfficers re : res) {
            String name = accountService.get_by_id(re.getAccountid()).getName();
            String projecttype = projectService.get_by_id(re.getProjectid()).getProjecttype();
            String projectname = projectService.get_by_id(re.getProjectid()).getProjectname();
            String projecttype2 = projectService.get_by_id(re.getProjectid()).getProjecttype();
            String standnumber = projectService.get_by_id(re.getProjectid()).getStandardnumber();
            Date training = re.getTrainingendtime();
            Date examinationendtime = re.getExaminationendtime();
            Map<String, String> temp = new HashMap<>();
            temp.put("人员",name);
            temp.put("类别(产品/项目/参数)",projecttype);
            temp.put("产品/项目/参数:名称",projectname);
            temp.put("标准名称",projecttype2);
            temp.put("标准编号（含年号）及条款号",standnumber);
            if(training == null){
                String training2="未完成";
                temp.put("培训完成时间", training2);
            }
            else{
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String training3 = sdf.format(training);
                temp.put("培训完成时间", training3);}
            if(examinationendtime == null){
                String examinationendtime2="未完成";
                temp.put("考核完成时间", examinationendtime2);
            }
            else{
                SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String  examinationendtime3 = sdf2.format(examinationendtime);
                temp.put("考核完成时间", examinationendtime3);}
            texp.add(temp);
        }
        return texp;
    }

    @Override
    public boolean change(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        UpdateWrapper<ProjectOfficers> updateWrapper2=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("training","Finish");
        Date now = new Date();
        updateWrapper2.eq("ProjectID",project_id).eq("AccountID",userid).set("TrainingEndTime",now);
        return update(updateWrapper)&&update(updateWrapper2);
    }
    @Override
    public boolean change_1(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("examination","Finish");
        return update(updateWrapper);
    }

    @Override
    public boolean change_p_0(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentPaperStatus","Unupload");
        return update(updateWrapper);
    }

    @Override
    public boolean change_p_1(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentPaperStatus","Audit");
        return update(updateWrapper);
    }

    @Override
    public boolean change_p_2(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentPaperStatus","Reject");
        return update(updateWrapper);
    }

    @Override
    public boolean change_p_3(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentPaperStatus","Pass");

        return update(updateWrapper);
    }

    @Override
    public boolean change_v_0(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentVideoStatus","Unupload");
        return update(updateWrapper);
    }

    @Override
    public boolean change_v_1(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentVideoStatus","Audit");
        return update(updateWrapper);
    }

    @Override
    public boolean change_v_2(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentVideoStatus","Reject");
        return update(updateWrapper);
    }

    @Override
    public boolean change_v_3(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("AssessmentVideoStatus","Pass");
        return update(updateWrapper);
    }

    @Override
    public boolean change_2(int project_id, int userid) {
        UpdateWrapper<ProjectOfficers> updateWrapper=new UpdateWrapper<>();
        UpdateWrapper<ProjectOfficers> updateWrapper2=new UpdateWrapper<>();
        updateWrapper.eq("ProjectID",project_id).eq("AccountID",userid).set("examination","Finish");
        Date now = new Date();
        updateWrapper2.eq("ProjectID",project_id).eq("AccountID",userid).set("ExaminationEndTime",now);
        return update(updateWrapper)&&update(updateWrapper2);
    }

    @Override
    public String get_write_msg(int project_id, int userid) {
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("AccountID",userid);
        return projectFileService.get_write(project_id,getOne(queryWrapper).getAssessmentpaper());
    }
    public String get_video_msg(int project_id, int userid) {
        QueryWrapper<ProjectOfficers> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("ProjectID",project_id);
        queryWrapper.eq("AccountID",userid);
        return projectFileService.get_write(project_id,getOne(queryWrapper).getAssessmentvideo());
    }

    @Override
    public List<Observe_2_DTO> get_observe_2(int user_id) {
        List<Observe_2_DTO> dto = new ArrayList<>();
        QueryWrapper<ProjectOfficers> queryWrapper = new QueryWrapper<>();
        if (user_id == 0) {
            List<ProjectOfficers> res = list(queryWrapper);
            for (ProjectOfficers re : res) {
                Observe_2_DTO dto1 = new Observe_2_DTO();
                dto1.setProject_id(re.getProjectid());
                dto1.setExamination(re.getExamination());
                dto1.setTraining(re.getTraining());
                dto1.setUser_id(re.getAccountid());
                Observe_DTO dto2;
                dto2 = accountService.observe(re.getAccountid());
                dto1.setContact(dto2.getContact());
                dto1.setName(dto2.getName());
                dto1.setAssessmentVideoStatus(re.getAssessmentvideostatus());
                dto1.setAssessmentPaperStatus(re.getAssessmentpaperstatus());
                dto.add(dto1);
            }
        }
        else {
            queryWrapper.eq("AccountID", user_id);
            List<ProjectOfficers> res = list(queryWrapper);
            for (ProjectOfficers re : res) {
                Observe_2_DTO dto1 = new Observe_2_DTO();
                dto1.setProject_id(re.getProjectid());
                dto1.setExamination(re.getExamination());
                dto1.setTraining(re.getTraining());
                dto1.setUser_id(re.getAccountid());
                Observe_DTO dto2;
                dto2 = accountService.observe(re.getAccountid());
                dto1.setContact(dto2.getContact());
                dto1.setName(dto2.getName());
                dto1.setAssessmentVideoStatus(re.getAssessmentvideostatus());
                dto1.setAssessmentPaperStatus(re.getAssessmentpaperstatus());
                dto.add(dto1);
            }
        }
        return dto;
    }

    @Override
    public boolean saveBatch(Collection<ProjectOfficers> entityList) {
        return super.saveBatch(entityList);
    }
}

